#
#===============================================================================
#
#         FILE:  Quote.pm
#
#  DESCRIPTION:  
#
#        FILES:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:   (), <>
#      COMPANY:  
#      VERSION:  1.0
#      CREATED:  04/14/2010 11:50:17 PM GMT
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

package GoogPl::Quote;

use LWP::UserAgent;
use Text::CSV;

sub register {
    my $self = shift;

    return ('cmd' => 'quote', 'pat' => '^!quote\s+([^ ]+)');
}

sub help {
    my $self = shift;
    return (
	    'Look up stock quotes via Yahoo Finance',
	   );
}

sub process_command {
    my $self = shift;
    my ($kernel, $sender, $nickhost, $target, $msgtxt, %conf) = @_;

    $msgtxt =~ /^!quote\s+([^ ]+)/;

    my $stock = uc($1);
    if ($stock eq '^DJI') { $stock = 'INDU'; }
    
    my $ua = LWP::UserAgent->new;
# f defined by http://www.gummy-stuff.org/Yahoo-data.htm
    my $url = "http://finance.yahoo.com/d/quotes.csv?s=$stock&f=snl1c1p2pohg";

    my $response = $ua->get($url);

    if (!$response->is_success) {
	return;
    }
    my $csv = Text::CSV->new;
    $csv->parse($response->decoded_content);

    my @fields = $csv->fields;
    # symbol / name / last trade / change / change in percent / previous close / open / day high / day low
    	
    $kernel->post($sender => privmsg => $target =>
	    sprintf('%s (%s) price: %s (%s/%s%%) close: %s open: %s high: %s low: %s',
		    @fields
		));
}

1;
